package com.bawei.exam57

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.room.Room
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.exam57.adpater.JfAdpater
import com.bawei.exam57.adpater.VpAdpater
import com.bawei.exam57.base.BaseViewActivity
import com.bawei.exam57.databinding.ActivityJfactivityBinding
import com.bawei.exam57.fragment.jf.AllFragment
import com.bawei.exam57.fragment.jf.IntoFragment
import com.bawei.exam57.fragment.jf.OutFragment
import com.bawei.exam57.room.JfEntity
import com.bawei.exam57.room.RoomUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.flyco.tablayout.CommonTabLayout
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import java.text.SimpleDateFormat
import java.util.ArrayList
@Route(path = "/app/JFActivity")
class JFActivity : BaseViewActivity<ActivityJfactivityBinding>() {
    val tablist = ArrayList<CustomTabEntity>()
    val list = ArrayList<Fragment>()
    lateinit var vpAdpater:VpAdpater
    val sdf= SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    override fun initView() {
        val jf = SPUtils.getInstance().getInt("jf", -1)
        if (jf==-1){
            binding.tvJf.text="积分：0"
            SPUtils.getInstance().put("jf",0)
        }else {
            binding.tvJf.text = "积分：" + jf
        }
        val tab1=MyTab("全部")
        val tab2=MyTab("积分收入")
        val tab3=MyTab("积分支出")
        tablist.add(tab1)
        tablist.add(tab2)
        tablist.add(tab3)
        binding.tab.setTabData(tablist)

        list.add(AllFragment())
        list.add(OutFragment())
        list.add(IntoFragment())
        vpAdpater = VpAdpater(this, list)
        binding.vp2.adapter=vpAdpater

        binding.tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                binding.vp2.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        binding.vp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.tab.currentTab = position
            }
        })

    }

    override fun initData() {
        binding.getJF.setOnClickListener{
            val millis = System.currentTimeMillis()
            val entity = JfEntity(sdf.format(millis), false, 46)
            RoomUtils.getJFDb().jfDao().insert(entity)
            binding.tvJf.text= "积分:"+(SPUtils.getInstance().getInt("jf")+46)
            SPUtils.getInstance().put("jf",SPUtils.getInstance().getInt("jf")+46)
            ToastUtils.showLong("积分添加成功")
            binding.vp2.adapter=vpAdpater
            jfoutandin()
        }

        jfoutandin()
    }

    private fun jfoutandin() {
        val jfAdpater= JfAdpater()
        val all = RoomUtils.getJFDb().jfDao().all()
        var into:Int=0
        var out:Int=0
        all.forEach {
            if (it.out_or_in){
                out+=it.jfupdata
            }else{
                into+=it.jfupdata
            }
        }
        binding.tvJfInto.text="积分收入：$into"
        binding.tvJfOut.text="积分支出：$out"
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_jfactivity
    }

    class MyTab(var title:String):CustomTabEntity{
        override fun getTabTitle(): String {
            return title
        }

        override fun getTabSelectedIcon(): Int {
            return 0
        }

        override fun getTabUnselectedIcon(): Int {
            return 0
        }
    }

}
